﻿using DoNetDrive.Common.Cryptography;
using DoNetDrive.Core.Command.Byte;
using DoNetDrive.Core.Command;
using DoNetDrive.Core.Connector.UDP;
using DoNetDrive.Core.Connector;
using DoNetDrive.Core;
using DoNetDrive.Protocol.OnlineAccess;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UDPAgency
{
    /// <summary>
    /// 代理端 -- 上端
    /// </summary>
    internal class IOAgencyHandle : INRequestHandle
    {
        ConnectorAllocator Allocator;

        private AgencyItem _AgencyItem;

        private OnlineAccessDecompile_PC mSendDecompile;
        private byte[] SoftwareFiles;
        private bool mBeginUpload;
        private uint OldCommandCode;
        private FileStream mSNLog;

        static IOAgencyHandle()
        {
            byte[] bKey = System.Text.Encoding.ASCII.GetBytes("5AE27EE392E3E2616CD2F181D7358119");
            RC4.IniRC4Key(ref bKey, bKey.Length, 1024);
        }

        public IOAgencyHandle(AgencyItem item)
        {
            _AgencyItem = item;
            Allocator = ConnectorAllocator.GetAllocator();
            mSendDecompile = new OnlineAccessDecompile_PC(DotNetty.Buffers.UnpooledByteBufferAllocator.Default);
        }

        public void Dispose()
        {
            Allocator = null;
            mSendDecompile.Dispose();
            mSendDecompile = null;
        }


        private string GetSN(OnlineAccessPacket pck)
        {
            return Encoding.ASCII.GetString(pck.SN);
        }

        private void WriteLog(string sLog)
        {
            if (mSNLog == null) return;

            byte[] bLog = System.Text.Encoding.ASCII.GetBytes(sLog + "\r\n");
            mSNLog.WriteAsync(bLog, 0, bLog.Length);
        }

        public void DisposeRequest(INConnector connector, IByteBuffer msg)
        {//接收

            //发送到下端
            int ilen = msg.ReadableBytes;
            byte[] bbuf = new byte[ilen];
            msg.ReadBytes(bbuf);
            var obuf = UnpooledByteBufferAllocator.Default.Buffer(msg.ReadableBytes);
            obuf.WriteBytes(bbuf);

            var sLogHex = ByteBufferUtil.HexDump(obuf).ToUpper();
            if (sLogHex.Length > 80)
                sLogHex = sLogHex.Substring(0, 80);

            //Trace.WriteLine($"PC端发送包 0x{sLogHex}");

            var udp = new UDPClientDetail(_AgencyItem.RemoteIP, _AgencyItem.RemotePort, 
                _AgencyItem.RemoteLocalIP, _AgencyItem.RemoteLocalPort);

            var cnl = Allocator.GetConnector(udp);
            if (cnl == null)
            {
                var dtl = new ByteCommandDetail(udp);

                var cmd = new ByteCommand(dtl, new ByteCommandParameter(obuf));

                Allocator.AddCommand(cmd);
            }
            else
            {
                //System.Threading.Thread.Sleep(2);
                cnl.WriteByteBuf(obuf);
            }
        }

        public void DisposeResponse(INConnector connector, IByteBuffer msg)
        {//发送
         //throw new NotImplementedException();
        }
    }
}
